/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.zzdt4j.core.executor.handler;

import cn.hutool.core.util.StrUtil;
import cn.zzdt4j.threadpool.dynamic.api.BootstrapPropertiesInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;
import org.springframework.boot.info.BuildProperties;

/**
 * Dynamic thread-pool banner handler
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023/10/18 15:54
 */
@Slf4j
public class DynamicThreadPoolBannerHandler implements InitializingBean {

    private final BootstrapPropertiesInterface bootstrapConfigProperties;

    private static final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: ";

    private static final String ZZDT4J_EMAIL = "email 954253184@qq.com";

    private static final String ZZDT4J_SITE = "site: https://github.com/AssumeJson";

    private static final int STRAP_LINE_SIZE = 50;

    private final String version;

    public DynamicThreadPoolBannerHandler(BootstrapPropertiesInterface bootstrapConfigProperties, BuildProperties buildProperties) {
        this.bootstrapConfigProperties = bootstrapConfigProperties;
        this.version = buildProperties != null ? buildProperties.getVersion() : "";
    }

    @Override
    public void afterPropertiesSet() {
        log.info("Dynamic thread-pool is running.....");
        printBanner();
    }

    private void printBanner() {
        String banner = " ________  ________  ________  _________  ___   ___        ___    ___  ___    \n" +
                "|\\_____  \\|\\_____  \\|\\   ___ \\|\\___   ___\\\\  \\ |\\  \\      |\\  \\  |\\  \\|\\  \\   \n" +
                " \\|___/  /|\\|___/  /\\ \\  \\_|\\ \\|___ \\  \\_\\ \\  \\\\_\\  \\     \\ \\  \\ \\ \\  \\ \\  \\  \n" +
                "     /  / /    /  / /\\ \\  \\ \\\\ \\   \\ \\  \\ \\ \\______  \\  __ \\ \\  \\ \\ \\  \\ \\  \\ \n" +
                "    /  /_/__  /  /_/__\\ \\  \\_\\\\ \\   \\ \\  \\ \\|_____|\\  \\|\\  \\\\_\\  \\ \\/  /\\/  /|\n" +
                "   |\\________\\\\________\\ \\_______\\   \\ \\__\\       \\ \\__\\ \\________\\/  ///  // \n" +
                "    \\|_______|\\|_______|\\|_______|    \\|__|        \\|__|\\|________/_ ///_ //  \n" +
                "                                                                 |__|/|__|/   \n";
        if (Boolean.TRUE.equals(bootstrapConfigProperties.getBanner())) {
            String bannerVersion = StrUtil.isNotEmpty(version) ? " (v" + version + ")" : "no version";
            StringBuilder padding = new StringBuilder();
            while (padding.length() < STRAP_LINE_SIZE - (bannerVersion.length() + DYNAMIC_THREAD_POOL.length())) {
                padding.append(" ");
            }
            System.out.println(AnsiOutput.toString(banner, AnsiColor.GREEN, "\n", DYNAMIC_THREAD_POOL, AnsiColor.DEFAULT,
                    padding.toString(), AnsiStyle.FAINT, bannerVersion, "\n\n", ZZDT4J_EMAIL, "\n", ZZDT4J_SITE, "\n"));
        }
    }

    /**
     * @return zdt4j version
     */
    public String getVersion() {
        return version;
    }
}
